package home.service.impl;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import net.sf.json.JSONObject;

import home.dao.UsersDao;
import home.model.Goods;
import home.model.PageBean;
import home.model.Users;
import home.model.minUsers;
import home.service.UsersService;

public class UsersServiceImpl implements UsersService{
	UsersDao usersDao;

	public UsersDao getUsersDao() {
		return usersDao;
	}

	public void setUsersDao(UsersDao usersDao) {
		this.usersDao = usersDao;
	}

	@Override
	public String encoderByMd5(String password) {
		// TODO Auto-generated method stub
		String resultString = null;
		resultString = new String(password);
		MessageDigest md;
		try
		{
			md = MessageDigest.getInstance("MD5");
			md.update(password.getBytes());
			byte[] digest = md.digest();
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < digest.length; i++)
			{
				sb.append(Integer.toHexString(((int) digest[i]) & 0xFF));
			}
			resultString = sb.toString();
		} catch (NoSuchAlgorithmException e)
		{
			e.printStackTrace();
		}
		return resultString;
	}

	@Override
	public boolean checklogin(Object value1, Object value2) {
		// TODO Auto-generated method stub
		List list = usersDao.findByUsersIdPwd(value1,value2);
		if(list == null)
			return false;
		else return true;
	}

	@Override
	public Users getUsers(String username) {
		// TODO Auto-generated method stub
		System.out.println("getUsers");
		Users users = usersDao.findByUsername(username);
		return users;
	}

	@Override
	public Users addUsers(String username, String password , String salt, String name,
			String main_type, String sex) {
		// TODO Auto-generated method stub
		Users users = usersDao.addUsers(username, password, salt, name, main_type, sex);
		if(users == null){
			return null;
		}
		return users;
	}

	@Override
	public List<Users> getAllUsers() {
		// TODO Auto-generated method stub
		List<Users> usersList = usersDao.getAllUsers();
		return usersList;
	}

	@Override
	public Users getUsers(int id) {
		// TODO Auto-generated method stub
		System.out.println("getUsers");
		Users users = usersDao.findById(id);
		return users;
	}

	@Override
	public boolean deleteUsersById(int id) {
		// TODO Auto-generated method stub
		System.out.println("deleteUsersById "+ id);
		boolean result = usersDao.deleteUsersById(id);
		return result;
	}

	@Override
	public boolean saveUsers(Users users) {
		// TODO Auto-generated method stub
		System.out.println("saveUsers");
		return usersDao.saveUsers(users);
	}

	@Override
	public String nadianSome(Users users, String type) {
		// TODO Auto-generated method stub
		System.out.println("nadianSome : " + type);
		return usersDao.nadianSome(users, type);
	}

	@Override
	public List<minUsers> searchUsers(String location) {
		// TODO Auto-generated method stub
		System.out.println("searchUsers : " + location);
		List<minUsers> usersList = usersDao.getAllMinUsers();
		List<minUsers> nearby = usersDao.searchUsers(usersList, location);
		return nearby;
	}

	@Override
	public Users randUsers() {
		// TODO Auto-generated method stub
		System.out.println("randUsers");;
		return usersDao.randUsers();
	}

	@Override
	public String winGetSome(Users win, Users lost, String type) {
		// TODO Auto-generated method stub
		System.out.println("winGetSome : " + type);
		return usersDao.winGetSome(win, lost, type);
	}

	@Override
	public PageBean queryForPage(int pageSize,int page, String like) {
		// TODO Auto-generated method stub
		String hql = "";
		if(like == null){
			hql = "from minUsers";
		}else{
			hql = "from minUsers u where u.username like '%" + like + "%'" 
					+ "or u.id like '%" + like + "%'"
					+ "or u.name like '%" + like + "%'";
		}
        
        int allRow = usersDao.getAllRowCount(hql);
        int totalPage = PageBean.countTotalPage(pageSize, allRow);
        final int offset = PageBean.countOffset(pageSize, page);
        final int length = pageSize;
        final int currentPage = PageBean.countCurrentPage(page);
        List<minUsers> list = usersDao.queryForPage(hql,offset, length);
        
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);    
        pageBean.setCurrentPage(currentPage);
        pageBean.setAllRow(allRow);
        pageBean.setTotalPage(totalPage);
        pageBean.setList(list);
        pageBean.init();
        return pageBean;
	}

	@Override
	public minUsers getMinUsers(int id) {
		// TODO Auto-generated method stub
		System.out.println("getMinUsers");
		minUsers users = usersDao.findByIdFromMinUsers(id);
		return users;
	}

	@Override
	public PageBean getTop(int page, String topType) {
		// TODO Auto-generated method stub
		String hql = "";
		int pageSize = 8;
			hql = "from minUsers u order by gameInfo." + topType + " desc";
        
        int allRow = usersDao.getAllRowCount(hql);
        int totalPage = PageBean.countTotalPage(pageSize, allRow);
        final int offset = PageBean.countOffset(pageSize, page);
        final int length = pageSize;
        final int currentPage = PageBean.countCurrentPage(page);
        List<minUsers> list = usersDao.queryForPage(hql,offset, length);
        
        System.out.println(list);
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);    
        pageBean.setCurrentPage(currentPage);
        pageBean.setAllRow(allRow);
        pageBean.setTotalPage(totalPage);
        pageBean.setList(list);
        pageBean.init();
        return pageBean;
	}
}
